<!--  -->
<template>
  <div>
    <s-table
      ref="table"
      rowKey="id"
      :columns="$auth('PROJECT.SYSTEM_ACCOUNT_LIST') ? columns : []"
      bordered
      :loading="loading"
      :data="loadData"
      :scroll="{ x: 1500 }">
      <!-- 表头区域 -->
      <template slot="title">
        <a-button style="margin-right: 16px" type="primary" @click="addVisible()" v-if="$auth('SYSTEM.SYSTEM_ACCOUNT_ADD')">新增成员</a-button>
        <a-button style="margin-right: 16px" type="default" icon="download" @click="clickExport" v-if="$auth('SYSTEM.SYSTEM_ACCOUNT_EXPORT')">导出</a-button>
        <a-button style="margin-right: 16px" type="dashed" icon="download" v-if="$auth('SYSTEM.SYSTEM_ACCOUNT_ADD')" :href="'/payment/muban/用户导入模板.xlsx' | dealImageUrl">模板下载</a-button>
        <a-upload
          ref="import"
          accept=".xlsx"
          name="file"
          list-type="picture"
          :preview-file="clickImport"
          v-if="$auth('SYSTEM.SYSTEM_ACCOUNT_ADD')"
        >
          <a-button type="default" icon="upload">导入</a-button>
        </a-upload>
        <!-- 查询条件 -->
        <div class="table-page-search-wrapper" style="margin-top: 20px">
          <a-form layout="inline">
            <a-row :gutter="48">
              <a-col :md="6" :sm="24">
                <a-form-item label="用户名">
                  <a-input v-model="queryParam.username" />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="姓名">
                  <a-input v-model="queryParam.name" />
                </a-form-item>
              </a-col>
              <template v-if="advanced">
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="账号:" labelAlign="left">
                    <a-input v-model="queryParam.account" />
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="手机号:" labelAlign="left">
                    <a-input v-model="queryParam.phone" />
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="角色:" labelAlign="left">
                    <a-select v-model="queryParam.roleId" allowClear>
                      <a-select-option v-for="item in rolesList" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="状态 :" labelAlign="left">
                    <a-select v-model="queryParam.status" allowClear>
                      <a-select-option :value="1">启用</a-select-option>
                      <a-select-option :value="-1">禁用</a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="是否管理员 :" labelAlign="left">
                    <a-select v-model="queryParam.isAdmin" allowClear>
                      <a-select-option :value="1">是</a-select-option>
                      <a-select-option :value="0">否</a-select-option>
                    </a-select>
                  </a-form-model-item>
                </a-col>
              </template>
              <a-col :md="(!advanced && 8) || 24" :sm="24">
                <span
                  class="table-page-search-submitButtons"
                  :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
                >
                  <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                  <a-button style="margin-left: 8px;" @click="resetQueryParam">重置</a-button>

                  <a @click="toggleAdvanced" style="margin-left: 8px;">
                    {{ advanced ? '收起' : '展开' }}
                    <a-icon :type="advanced ? 'up' : 'down'" />
                  </a>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
      </template>
      <!-- 用户类型 -->
      <template slot="type" slot-scope="text, record">
        <span v-if="record.type == 1">个人</span>
        <span v-if="record.type == 2">组织</span>
      </template>
      <!-- 状态 -->
      <template slot="status" slot-scope="text, record">
        <a-tag v-if="record.status == -1" color="#f50">禁用</a-tag>
        <a-tag v-if="record.status == 1" color="#87d068">启用</a-tag>
      </template>
      <!-- 操作区域 -->
      <template slot="isAdmin" slot-scope="text, record">
        <span v-if="record.isAdmin === 1">是</span>
        <span v-else>否</span>
      </template>
      <template slot="operation" slot-scope="text, record">
        <a-space>
          <a-button v-if="record.status == 1 && $auth('SYSTEM.SYSTEM_ACCOUNT_STATUS')" type="warning" size="small" @click="activeDisable(-1, record.id)">
            {{ record.status !== -1 ? '禁用' : '启用' }}
          </a-button>
          <a-button v-if="record.status == -1 && $auth('SYSTEM.SYSTEM_ACCOUNT_STATUS')" type="warning" size="small" @click="activeDisable(1, record.id)">
            {{ record.status !== 1 ? '启用' : '禁用' }}
          </a-button>
          <a-button type="primary" size="small" @click="activeEdit(record, false)" v-if="!$auth('SYSTEM.SYSTEM_ACCOUNT_EDIT') && $auth('SYSTEM.SYSTEM_ACCOUNT_DETAIL') ">查看</a-button>
          <a-button type="primary" size="small" @click="activeEdit(record, true)" v-if="$auth('SYSTEM.SYSTEM_ACCOUNT_EDIT')">编辑</a-button>
          <a-popconfirm title="是否确认要重置密码？" ok-text="确定" cancel-text="取消" @confirm="resetPwd(record.id)">
            <a-button v-if="isAdmin" type="danger" size="small" >重置密码</a-button>
          </a-popconfirm>
        </a-space>
      </template>
    </s-table>

    <!-- 抽屉add -->
    <AddDrawer ref="addDrawerRef" :visible="drawerVisible" @closeDrawer="closeDrawer" />
    <!-- 编辑抽屉 -->
    <EditDrawer
      ref="editDrawer"
      :is-edit="isEdit"
      :editDrawVisible="editDrawVisible"
      @closeEditDrawer="closeEditDrawer" />
  </div>
</template>

<script>
import store from '@/store'
import monitor from '@/utils/monitor'
import user from '@/monitor/views/system/user.js'
import { exportExcel } from '@/utils/util'

import AddDrawer from './components/addDrawer'
import EditDrawer from './components/editDrawer'
import { getUserList, switchStatus, userImport, userExport, getRolesList, resetPwd } from '@/api/system/user'
import { buildPaginationObj } from '@/components/Table'
const columns = [
  {
    title: '用户编号',
    dataIndex: 'userNo'
  },
  {
    title: '用户名',
    dataIndex: 'userName'
  },
  {
    title: '姓名',
    dataIndex: 'name'
  },
  {
    title: '账号',
    dataIndex: 'account'
  },
  {
    title: '邮箱',
    dataIndex: 'email'
  },
  {
    title: '手机号',
    dataIndex: 'phone'
  },
  {
    title: '职务',
    dataIndex: 'position'
  },
  {
    title: '角色名',
    dataIndex: 'roleName'
  },
  // {
  //   title: '电子签名',
  //   dataIndex: 'esignUrl'
  // },
  {
    title: '用户类型',
    dataIndex: 'type',
    scopedSlots: { customRender: 'type' }
  },
  {
    title: '状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '是否管理员',
    dataIndex: 'isAdmin',
    scopedSlots: { customRender: 'isAdmin' }
  },
  {
    title: '操作',
    dataIndex: 'operation',
    fixed: 'right',
    width: 250,
    scopedSlots: { customRender: 'operation' }
  }
]
const watch = monitor(user)
export default {
  name: '',
  components: {
    AddDrawer,
    EditDrawer
  },
  data () {
    return {
      drawerVisible: false,
      editDrawVisible: false,
      columns,
      loading: false,
      queryParam: {
        username: null,
        name: null,
        account: null,
        phone: null,
        roleId: null,
        status: null,
        isAdmin: ''
      },
      rolesList: [],
      advanced: false,
      loadData: parameter => {
        if (this.$auth('PROJECT.SYSTEM_ACCOUNT_LIST')) {
          return getUserList(Object.assign(parameter, this.queryParam)).then(res => {
            return res
          })
        } else {
          return Promise.resolve(buildPaginationObj([]))
        }
      },
      isEdit: false
    }
  },
  created () {
    this.isAdmin = store.getters.userInfo && store.getters.userInfo.id === 1
    this.getRolesList()
  },
  methods: watch({
    // 获取角色列表
    getRolesList () {
      getRolesList()
        .then(res => {
          this.rolesList = res
        })
    },
    // 点击展开 详细搜索信息
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    // 点击重置
    resetQueryParam () {
      this.queryParam = this.$options.data().queryParam
      this.$refs.table.refresh(true)
    },
    // 新增抽屉
    addVisible () {
      this.drawerVisible = true
    },
    // 编辑当前行
    activeEdit (row, flag) {
      this.isEdit = flag
      const rowObj = JSON.parse(JSON.stringify(row))
      this.$refs.editDrawer.isAdminName = rowObj.roleName
      delete rowObj.createTime
      delete rowObj.roleName
      this.$refs.editDrawer.form = rowObj
      console.log(rowObj, '当前对象')
      this.$refs.editDrawer.echoDepartment()
      this.$refs.editDrawer.judgeType()
      this.editDrawVisible = true
      this.editUser()
      this.editAccount()
    },
    // 启用禁用操作
    activeDisable (flag, id) {
      switchStatus({
        id,
        status: flag
      }).then(res => {
        if (flag === 1) {
          this.enable()
        } else {
          this.forbidden()
        }
        this.$message.success('操作成功')
        this.$refs.table.loadData()
      })
    },
    // 新增对话框关闭 刷新列表
    closeDrawer (val) {
      this.drawerVisible = false
      if (val) {
        this.$refs.table.loadData()
      }
    },
    // 编辑对话框关闭 刷新列表
    closeEditDrawer (val) {
      this.editDrawVisible = false
      if (val) {
        console.log('sss', this.$refs.table)
        this.$refs.table.loadData()
      }
    },
    clickExport () {
      this.exportUser()
      this.exportAccount()
      const exportParams = this.queryParam
      userExport(exportParams).then(res => {
        console.log(res, 'export')
        exportExcel('账户信息', res)
      })
    },
    clickImport (file) {
      const isJpgOrPng = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
      if (!isJpgOrPng) {
        this.$notification.error({
          message: file.name,
          description: '仅支持文件格式: .xlsx 格式文件!'
        })
        return false
      } else {
        const params = new FormData()
        params.append('file', file)
        return userImport(params).then(res => {
          this.$notification.success({
            message: file.name,
            description: '上传成功'
          })
          this.importUser()
          this.importAccount()
          console.log(res)
        }).finally(() => {
          this.$refs.import.sFileList = []
          this.$refs.table.loadData()
        })
      }
    },
    // 新增成员和编辑成员组件重新请求获取角色接口
    refreshListData () {
      this.$nextTick(() => {
        this.$refs.addDrawerRef.getInterfaceData()
        this.$refs.editDrawer.getInterfaceData()
      })
    },
    // 导入用户埋点
    importUser () {},
    // 导入账户埋点
    importAccount () {},
    // 导出用户埋点
    exportUser () {},
    // 导出账户埋点
    exportAccount () {},
    // 编辑用户埋点
    editUser () {},
    // 编辑账户埋点
    editAccount () {},
    // 禁用账户
    forbidden () {},
    // 启用账户
    enable () {},
    // 重置密码
    resetPwd (id) {
      resetPwd({ id }).then(res => {
        this.$message.success('操作成功')
      })
    }
  })
}
</script>
<style scoped lang="scss"></style>
